//
//  MemoryView.m
//  gong
//
//  Created by echo on 14-2-23.
//  Copyright (c) 2014年 echo. All rights reserved.
//

#import "MemoryView.h"
#import "UICreator.h"
#import "PositionTools.h"
#import "GameManager.h"
#import "AutoRemoveMessageView.h"
#import "TaskManager.h"
#import "Constants.h"

@implementation MemoryView
{
    UIImageView *_cover;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createCover];
    }
    return self;
}

- (void)createCover
{
    _cover = [UICreator createImageViewFromImagename:@"cover.jpg"];
    [PositionTools placeView:_cover atTheCenterOfTheView:self];
    _cover.userInteractionEnabled = YES;
    
    
    UIButton *exit = [UICreator createButtonWithNormalImage:@"exit.png"
                                           highlightedImage:nil
                                                     target:self
                                                     action:@selector(goBack)
                                                      use2X:YES];
    [PositionTools placeView:exit atTheLeftTopOfTheView:_cover offset:CGSizeMake(20, 20)];
    
    CGRect buttonFrame = CGRectMake(0, 0, 800, 100);
    UIColor *buttonColor = [UIColor whiteColor];
    UIFont *buttonFont = [UIFont systemFontOfSize:40];
    UIButton *memory1 = [UICreator createButtonWithTitle:@"回忆1(无存档)"
                                                  titleColor:buttonColor
                                                        font:buttonFont
                                                       frame:buttonFrame
                                                  buttonType:UIButtonTypeCustom
                                                      target:self action:@selector(toLoadGame:)];
    memory1.tag = 0;
    
    UIButton *memory2 = [UICreator createButtonWithTitle:@"回忆2(无存档)"
                                              titleColor:buttonColor
                                                    font:buttonFont
                                                   frame:buttonFrame
                                              buttonType:UIButtonTypeCustom
                                                  target:self action:@selector(toLoadGame:)];
    memory2.tag = 1;
    
    UIButton *memory3 = [UICreator createButtonWithTitle:@"回忆3(无存档)"
                                              titleColor:buttonColor
                                                    font:buttonFont
                                                   frame:buttonFrame
                                              buttonType:UIButtonTypeCustom
                                                  target:self action:@selector(toLoadGame:)];
    memory3.tag = 2;

    [PositionTools placeView:memory1 atTheTopMiddleOfTheView:_cover offset:150];
    [PositionTools placeView:memory2 atTheTopMiddleOfTheView:_cover offset:350];
    [PositionTools placeView:memory3 atTheTopMiddleOfTheView:_cover offset:550];
    
    [UICreator makeGameButton:memory1];
    [UICreator makeGameButton:memory2];
    [UICreator makeGameButton:memory3];
    
    NSArray *memoryButtonArray = @[memory1, memory2, memory3];
    
    NSArray *memoryArray = [GameManager sharedInstance].memoryArray;
    for (int i=0; i<3; i++) {
        for (Memory* memory in memoryArray) {
            UIButton *button = memoryButtonArray[i];
            if (memory.saveIndex == (i + 1)) {
                NSString *placeName = memory.isInStreet ? @"大街" : memory.houseName;
                NSString *newTitle = [NSString stringWithFormat:@"回忆%i(%@ %@)", i+1, memory.saveTime, placeName];
                [button setTitle:newTitle forState:UIControlStateNormal];
            }
        }
    }
}

- (void)goBack
{
    self.alpha = 1;
    [UIView animateWithDuration:.5 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)toLoadGame:(UIButton*)sender
{
    NSInteger memoryIndex = sender.tag + 1;
    
    BOOL hasMemory = NO;
    NSArray *memoryArray = [GameManager sharedInstance].memoryArray;
    for (Memory* memory in memoryArray) {
        if (memory.saveIndex == memoryIndex) {
            hasMemory = YES;
            
            [[NSUserDefaults standardUserDefaults] setInteger:memory.taskIndex forKey:TaskIndexKey];
            [[TaskManager sharedInstance] initTaskStatus];

            break;
        }
    }
    
    if (hasMemory) {
        if (self.loadGame) {
            self.loadGame(sender.tag);
        }
    } else {
        [AutoRemoveMessageView show:@"无回忆" withContainerView:self completion:nil];
    }
}

@end
